#!/bin/sh
#
# This is the script for generating files for a specific Dalton test job.
#
# For the .check file ksh or bash is preferred, otherwise use sh
# (and hope it is not the old Bourne shell, which will not work)
#
if [ -x /bin/ksh ]; then
   CHECK_SHELL='#!/bin/ksh'
elif [ -x /bin/bash ]; then
   CHECK_SHELL='#!/bin/bash'
else
   CHECK_SHELL='#!/bin/sh'
fi


#######################################################################
#  TEST DESCRIPTION
#######################################################################
cat > energy_scsmp2.info <<'%EOF%'
   energy_scsmp2
   -------------
   Molecule:         H2O
   Wave Functions:   RHF, SCS-MP2 / cc-pVDZ
   Test Purpose:     Check SCS-MP2 energy calculation with symmetry
%EOF%

#######################################################################
#  MOLECULE INPUT
#######################################################################
cat > energy_scsmp2.mol <<'%EOF%'
BASIS
cc-pVDZ
   Wave Functions:   RHF, SOS-MP2, and MCSCF (CAS) / cc-pVDZ
   Test Purpose:     Check SCS-MP2 energy calculation with symmetry
    2
        8.    1
O     0.0 -0.2249058930 0.0
        1.    2
H_a   1.45235 0.899623 0.0
H_b  -1.45235 0.899623 0.0
%EOF%

#######################################################################
#  DALTON INPUT
#######################################################################
cat > energy_scsmp2.dal <<'%EOF%'
**DALTON INPUT
.RUN WAVE FUNCTION
**WAVE FUNCTIONS
.HF
.MP2
*MP2 INPUT
.SCSMP2
*CONFIGURATION INPUT
.SYMMETRY
 1
.SPIN MUL
 1
.INACTIVE
 1 0 0 0
.ELECTRONS
 8
.CAS SPACE
 4 2 2 0
*PRINT LEVELS
.PRINTF
 1 0
 51
# p6flag(51) is code for checking that reduced matrix redH is symmetric
**END OF DALTON INPUT
%EOF%
#######################################################################



#######################################################################

#######################################################################
#  CHECK SCRIPT
#######################################################################
echo $CHECK_SHELL >energy_scsmp2.check
cat >>energy_scsmp2.check <<'%EOF%'
log=$1

if [ `uname` = Linux ]; then
   GREP="egrep -i -a"
else
   GREP="egrep -i"
fi

# Reading from basis set library:
CRIT1=`$GREP "Basis set used is \"cc-pVDZ\" from the basis set library\." $log | wc -l`
TEST[1]=`expr $CRIT1`
CTRL[1]=1
ERROR[1]="BASIS SET LIBRARY NOT SET UP CORRECTLY"

# Reading molecular geometry:
CRIT1=`$GREP "1 * x * (0| )\.0000000000" $log | wc -l`
CRIT2=`$GREP "2 * y * (0| )\.0000000000" $log | wc -l`
CRIT3=`$GREP "3 * z * (\-0|\-)\.1258515023" $log | wc -l`
CRIT4=`$GREP "4 * x * (0| )\.0000000000" $log | wc -l`
CRIT5=`$GREP "5 * y * 1\.4523500000" $log | wc -l`
CRIT6=`$GREP "6 * z * (0| )\.9986773907" $log | wc -l`
CRIT7=`$GREP "7 * x * (0| )\.0000000000" $log | wc -l`
CRIT8=`$GREP "8 * y * \-1\.4523500000" $log | wc -l`
CRIT9=`$GREP "9 * z * (0| )\.9986773907" $log | wc -l`
CRIT10=`$GREP "Total number of coordinates: * 9" $log | wc -l`
TEST[2]=`expr $CRIT1  \+ $CRIT2  \+ $CRIT3  \+ $CRIT4  \+ $CRIT5  \+ \
              $CRIT6  \+ $CRIT7  \+ $CRIT8  \+ $CRIT9  \+ $CRIT10`
CTRL[2]=10
ERROR[2]="MOLECULAR GEOMETRY NOT READ CORRECTLY"

# Nuclear repulsion energy:
CRIT1=`$GREP "Nuclear repulsion energy : * 9.0550045256[0-9][0-9]" $log | wc -l`
TEST[3]=`expr $CRIT1`
CTRL[3]=1
ERROR[3]="NUCLEAR REPULSION ENERGY NOT CORRECT"

# Wave function:
CRIT1=`$GREP "Number of closed shell electrons * 10" $log | wc -l`
CRIT2=`$GREP "Number of electrons in active shells * 0" $log | wc -l`
CRIT3=`$GREP "Wave function type .* SCS-MP2" $log | wc -l`
CRIT4=`$GREP "Total number of orbitals * 24" $log | wc -l`
CRIT5=`$GREP "Spin multiplicity .* 1  * 0" $log | wc -l`
CRIT6=`$GREP "Total number of symmetries * 4" $log | wc -l`
CRIT7=`$GREP "Reference state symmetry * 1" $log | wc -l`
TEST[4]=`expr $CRIT1  \+ $CRIT2  \+ $CRIT3  \+ $CRIT4  \+ $CRIT5  \+ \
              $CRIT6  \+ $CRIT7`
CTRL[4]=7
ERROR[4]="INCORRECT WAVE FUNCTION"

# Symmetry:
CRIT1=`$GREP "Number of coordinates in each symmetry: .* 3 * 2 * 3 * 1" $log | wc -l`
CRIT2=`$GREP "Number of orbitals in each symmetry: .* 11 * 4 * 7 * 2" $log | wc -l`
CRIT3=1 # `$GREP "Inactive orbitals .* 1 * 0 * 0 * 0" $log | wc -l`
CRIT4=1 # `$GREP "Active orbitals .* 4 * 2 * 2 * 0" $log | wc -l`
CRIT5=`$GREP "Occupied SCF orbitals .* 3 * 1 * 1 * 0" $log | wc -l`
TEST[5]=`expr $CRIT1  \+ $CRIT2  \+ $CRIT3  \+ $CRIT4  \+ $CRIT5`
CTRL[5]=5
ERROR[5]="SYMMETRY IS NOT CORRECT"

# SCF energy:
CRIT1=`$GREP "Hartree-Fock total energy *: * \-76\.025681483" $log | wc -l`
TEST[6]=`expr $CRIT1`
CTRL[6]=1
ERROR[6]="SCF ENERGY NOT CORRECT"

# MP2 energy:
# hjaaj Jan 05: decreased number of digits as MP2 energy is only of lin.acc.
CRIT1=` $GREP "Scaled MP2 second order energy *: * \-76\.2268745" $log | wc -l` # SCS-MP2 energy
#CRIT1=`$GREP "MP2 second order energy *: * \-76\.2306668" $log | wc -l` # normal MP2 energy

TEST[7]=`expr $CRIT1`
CTRL[7]=1
ERROR[7]="SCS-MP2 ENERGY NOT CORRECT"

TEST[8]=0
CTRL[8]=0
ERROR[8]="TEST 8 NOT CORRECT"

# Occupancy of orbitals:
CRIT1=`$GREP "1.9999187. * 1.9865298. * 1.9704430. * 0*.0216386. * 0*.0102709." $log | wc -l`
CRIT2=`$GREP "1.9752325. * 0*.0165969. * 0*.0037653. * 0*.0005046." $log | wc -l`
CRIT3=`$GREP "1.9680919. * 0*.0238841. * 0*.0052212. * 0*.0009503. * 0*.0006122." $log | wc -l`
CRIT4=`$GREP "0*.0043429. * 0*.0006666." $log | wc -l`
TEST[9]=`expr $CRIT1 \+ $CRIT2 \+ $CRIT3 \+ $CRIT4`
CTRL[9]=4
ERROR[9]="MP2 NATORB OCCUPANCY NOT CORRECT"

# Check for WARNING for .mol line longer than 80 char
#  WARNING -- line in ".mol" file truncated, line     5
#CRIT1=`$GREP "WARNING .* file truncated, line * 5" $log | wc -l`
#TEST[10]=`expr $CRIT1`
#CTRL[10]=1
#ERROR[10]="WARNING FOR LONG .MOL LINE NOT CORRECT"

PASSED=1
for i in 1 2 3 4 5 6 7 8 9
do
   if [ ${TEST[i]} -ne ${CTRL[i]} ]; then
     echo "${ERROR[i]} ( test = ${TEST[i]}; control = ${CTRL[i]} ); "
     PASSED=0
   fi
done

if [ $PASSED -eq 1 ]
then
  echo TEST ENDED PROPERLY
  exit 0
else
  echo THERE IS A PROBLEM
  exit 1
fi

%EOF%
#######################################################################
